//
//  XFeedbackViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/9/4.
//

#import "XFeedbackViewController.h"
#import "XTextField.h"
#import "RFeedbackRequest.h"
@interface XFeedbackViewController ()<UITextFieldDelegate,UITextViewDelegate>

/// 下方视图承载
@property(nonatomic,strong)MyLinearLayout *contentLayout;

/// 选项
@property(nonatomic,strong)MyFlowLayout *flowLayout;

/// 反馈内容
@property(nonatomic,strong)UITextView *textView;

/// 占位文本
@property (nonatomic,weak)UILabel* placeLb;

/// 字数
@property (nonatomic,weak)UILabel* numLb;

/// 联系方式
@property(nonatomic,strong)XTextField *contactTextField;

@end

@implementation XFeedbackViewController

- (void)loadView{
    [super loadView];
   
    self.view.backgroundColor = COLOR_BACKGEOUND;
    // 根视图
    MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    rootLayout.myMargin = 0;
    [self.view addSubview:rootLayout];
    

    //下方滚动先行视图
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.myMargin = 0;
    scrollView.weight = 1.0;
    [rootLayout addSubview:scrollView];
    
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contentLayout.myTop = 0;
    contentLayout.myHorzMargin = 0;
    contentLayout.heightSize.lBound(scrollView.heightSize, 10, 1);
    [scrollView addSubview:contentLayout];
    _contentLayout = contentLayout;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavBar];
    
    [self initSubViews];
    
    NSArray* tags = @[@"功能建议",@"计数问题",@"打卡问题",@"性能问题",@"内容问题",@"其他"];
    
    [self setuptags:tags];
    
    // Do any additional setup after loading the view.
}

- (void)setupNavBar{
    self.navigationItem.title = @"问题反馈";
    
    UIBarButtonItem* saveItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:(UIBarButtonItemStylePlain) target:self action:@selector(sumbit)];
    self.navigationItem.rightBarButtonItem = saveItem;
}

- (void)initSubViews{
    
    UILabel* lb1 = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_BLACK_DARK text:@"您反馈的是:"];
    lb1.myLeft = 10;
    lb1.myTop = 20;
    [self.contentLayout addSubview:lb1];
    
    // 流式布局
    MyFlowLayout* flowLayout = [MyFlowLayout flowLayoutWithOrientation:MyOrientation_Vert arrangedCount:0];
    flowLayout.backgroundColor = UIColor.whiteColor;
    flowLayout.myHeight = MyLayoutSize.wrap;
    flowLayout.subviewSpace = 16;
    flowLayout.padding = UIEdgeInsetsMake(16, 16,16, 16);
    flowLayout.myHorzMargin = 10;
    flowLayout.myTop = 8;
    flowLayout.layer.cornerRadius = PX(2);
    flowLayout.layer.masksToBounds = YES;
    [self.contentLayout addSubview:flowLayout];
    _flowLayout = flowLayout;
    
    
    
    UILabel* lb2 = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_BLACK_DARK text:@"反馈问题:"];
    lb2.myLeft = 10;
    lb2.myTop = 20;
    [self.contentLayout addSubview:lb2];
    
    MyRelativeLayout* subLayout = [MyRelativeLayout new];
    subLayout.backgroundColor = UIColor.whiteColor;
    subLayout.myHorzMargin = 10;
    subLayout.myTop = 8;
    subLayout.myHeight = MyLayoutSize.wrap;
    subLayout.layer.cornerRadius = PX(2);
    subLayout.layer.masksToBounds = YES;
    [self.contentLayout addSubview:subLayout];
    
    UITextView* textView = [UITextView new];
    textView.backgroundColor = UIColor.whiteColor;
    textView.font = FONT_FIT(14);
    textView.textColor = COLOR_BLACK_DARK;
    textView.delegate = self;
    textView.scrollEnabled = NO;
    textView.returnKeyType = UIReturnKeyDone;
    textView.showsHorizontalScrollIndicator = NO;
    textView.showsVerticalScrollIndicator = NO;
    textView.tintColor = COLOR_THEME_A;
    textView.myMargin = 0;
    textView.myHeight = PX(100);
    [subLayout addSubview:textView];
    _textView = textView;
    
    UILabel* placeLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_GRAY_QUITE text:@"请输入反馈类容(最少10字)"];
    placeLb.topPos.equalTo(textView.topPos).offset(4);
    placeLb.leftPos.equalTo(textView.leftPos).offset(4);
    [subLayout addSubview:placeLb];
    _placeLb = placeLb;
    
    UILabel* numLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_QUITE text:@"0/100"];
    numLb.myRight = 0;
    numLb.topPos.equalTo(textView.bottomPos).offset(4);
    [subLayout addSubview:numLb];
    _numLb = numLb;
    
    NSString* text = @"* 如果有好的建议或者意见，可以填写QQ或者手机号，方便我们主动联系您";
//    if(self.type == 1){
//        text = @"* 如果有好的建议或者意见，可以填写QQ或者手机号，方便我们主动联系您";
//    }else{
//        text = @"* 如果是播放卡顿，请尽量写上城市和运营商，以便更好的解决您的问题";
//    }
    UILabel* lb3 = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:text];
    lb3.myHeight = MyLayoutSize.wrap;
    lb3.myHorzMargin = 10;
    lb3.myTop = 20;
    [self.contentLayout addSubview:lb3];
    
    
    XTextField* contactTextField = [[XTextField alloc] init];
    contactTextField.myHorzMargin = 10;
    contactTextField.myTop = 8;
    contactTextField.myHeight = PX(44);
    contactTextField.textField.placeholder = @"请填写手机或QQ号";
    contactTextField.textField.keyboardType = UIKeyboardTypePhonePad;
    contactTextField.textField.tintColor = COLOR_THEME_A;
//    if(self.type == 1){
//        contactTextField.textField.tintColor = COLOR_THEME_A;
//    }else{
//        contactTextField.textField.tintColor = COLOR_THEME;
//    }
    [self.contentLayout addSubview:contactTextField];
    _contactTextField = contactTextField;
}

- (void)setuptags:(NSArray*)tags{

    for (int i = 0; i < tags.count; i++) {
        NSString* tag = tags[i];
        
        UIButton *tagButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [tagButton setTitle:tag forState:UIControlStateNormal];
        [tagButton setTitleColor:COLOR_GRAY_BASE forState:UIControlStateNormal];
        [tagButton setTitleColor:COLOR_WHITE forState:UIControlStateSelected];
        tagButton.titleLabel.font = FONT_FIT(12);
        tagButton.backgroundColor = COLOR_BACKGEOUND;
        tagButton.layer.cornerRadius = PX(11);
        tagButton.widthSize.equalTo(@(MyLayoutSize.wrap)).add(16).min(PX(40));
        tagButton.heightSize.equalTo(@(PX(22)));
        tagButton.tag = 100+i;
        [tagButton sizeToFit];
        [tagButton addTarget:self action:@selector(tagClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.flowLayout addSubview:tagButton];
    }
}

#pragma mark  -- action
- (void)sumbit{
    
    NSString* text = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if(text.length < 10){
        [Toast toast:@"你输入的内容太少了"];
        return;
    }
    
    RFeedbackRequest* request = [RFeedbackRequest new];
    request.advice = text;
    request.action = @"n";
    [RequestOperation encryptedRequest:request quite:YES callBack:NULL];
    
    [self closeViewControllerAnimated:YES];
    [Toast toast:@"反馈成功"];
}

- (void)tagClick:(UIButton*)sender{

//    if(self.type == 1){
        for (UIButton* btn in self.flowLayout.subviews) {
            if(btn == sender){
                btn.selected = YES;
                btn.backgroundColor = COLOR_THEME_A;
            }else{
                btn.selected = NO;
                btn.backgroundColor = COLOR_BACKGEOUND;
            }
        }
//    }else{
//        for (UIButton* btn in self.flowLayout.subviews) {
//            if(btn == sender){
//                btn.selected = YES;
//                btn.backgroundColor = COLOR_THEME;
//            }else{
//                btn.selected = NO;
//                btn.backgroundColor = COLOR_BACKGEOUND;
//            }
//        }
//    }
    
}

#pragma mark  -- UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView{
    
    self.placeLb.visibility = textView.text.length?MyVisibility_Gone:MyVisibility_Visible;
    [self.placeLb sizeToFit];
    
    if(textView.markedTextRange != NULL){
        return;
    }
    
    NSString* text = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if(text.length > 200){
        text = [text substringToIndex:200];
        textView.text = text;
    }
   
    self.numLb.text = [NSString stringWithFormat:@"%ld/%ld",text.length,200];
    [self.numLb sizeToFit];
}

@end
